package linshuai;

import javafx.application.Platform;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.ProgressBar;

public class UI extends HBox
{
	public UI(Stage stage) {
		CAModel model = new CAModel();
		CAView canvas = new CAView();

		Timeseries densities = new Timeseries(1);
		densities.setTitle("Densities:");
		densities.setup(new String[] {"HIV", "CD4", "CD8"});
		densities.setPrefSize(300, 150);

		Timeseries strains = new Timeseries(8);
		strains.setTitle(String.format("Strains number: (max: %d)",
			(int)Math.pow(2,CAModel.l)));
		strains.setup(new String[] {"HIV", "CD4", "CD8"});
		strains.setPrefSize(300, 100);

		ProgressBar progress = new ProgressBar();
		progress.setMaxWidth(Double.MAX_VALUE);
		VBox chartsbox = new VBox();
		chartsbox.getChildren().addAll(strains,densities,progress);
		BorderPane chartsbottom = new BorderPane();
		chartsbottom.setBottom(chartsbox);

		getChildren().addAll(canvas,chartsbottom);

		final int until = 40*365;

		class UIRun implements Runnable {
			public void run() {
				if(model.getTicks()%10 == 0) {
					canvas.draw(model);
					densities.add(model.getTicks(), model.getDensities());
					strains.add(model.getTicks(), model.getStrainsCounts());

					stage.setTitle(String.format("Days: %d", model.getTicks()));
					progress.setProgress(((double)model.getTicks())/until);
				}
				model.step();
				if(model.getTicks() < until)
					Platform.runLater(this);  // rinse and repeat :)
				else
					progress.setVisible(false);
			}
		}
		Platform.runLater(new UIRun());
	}
}
